package cn.jy.careuplusdoctor.utils;

import java.io.IOException;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.sql.Clob;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.util.Base64;


/**
 * 字符串工?
 * 
 * @author Eriksson
 * 
 */
public class StringUtil {

	/**
	 * 对字符串进行Url编码
	 * 
	 * @param str
	 * @param enc
	 * @return
	 */
	public static String urlEncode(String str, String enc) {
		String result = "";
		try {
			result = URLEncoder.encode(str, enc);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 对字符串进行Url解码
	 * 
	 * @param str
	 * @param enc
	 * @return
	 */
	public static String urlDecode(String str, String enc) {
		String result = "";
		try {
			result = URLDecoder.decode(str, enc);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 判断字符串是否数?
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isNumber(String str) {
		boolean bol = false;
		if (str == null || "".equals(str)) {
			return bol;
		}
		Pattern pattern = Pattern.compile("^[-]?[0-9]+.{0,1}[0-9]*$");
		Matcher matcher = pattern.matcher(str);
		bol = matcher.matches();
		return bol;
	}
	
	/**
	 * 判断字符串是否JSON数组字符?
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isJSONArray(String str) {
		boolean bol = false;
		if (str == null || "".equals(str)) {
			return bol;
		}
		bol = str.charAt(0)=='['&&str.charAt(str.length()-1)==']';
		return bol;
	}
	
	/**
	 * 判断字符串是否JSON字符?
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isJSON(String str) {
		boolean bol = false;
		if (str == null || "".equals(str)) {
			return bol;
		}
		bol = str.charAt(0)=='{'&&str.charAt(str.length()-1)=='}';
		return bol;
	}

	/**
	 * 是否不包含中?
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isNotChinese(String str) {
		boolean bol = false;
		if (str == null || "".equals(str)) {
			return bol;
		}
		Pattern pattern = Pattern.compile("[^\u4e00-\u9fa5]*");
		Matcher matcher = pattern.matcher(str);
		bol = matcher.matches();
		return bol;
	}

	/**
	 * 判断字符串是否为?
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isNotEmpty(String str) {
		boolean bol = false;
		bol = str != null && !"".equals(str);
		return bol;
	}

	/**
	 * 判断字符串是否为日期
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isDateTime(String str) {
		boolean bol = false;
		if (str == null || "".equals(str)) {
			return bol;
		}
		Pattern pattern = Pattern
				.compile("^((\\d{2}(([02468][048])|([13579][26]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][1235679])|([13579][01345789]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))(\\s(((0?[0-9])|([1-2][0-3]))\\:([0-5]?[0-9])((\\s)|(\\:([0-5]?[0-9])))))?$");
		Matcher matcher = pattern.matcher(str);
		bol = matcher.matches();
		return bol;
	}

	/**
	 * 转换为字符串（如为空则返回null?
	 * 
	 * @param obj
	 * @return
	 */
	public static String parseString(Object obj) {
		String str = null;
		str = String.valueOf(obj);
		if (str == null || "null".equals(str.toLowerCase())) {
			return "";
		}
		return str;
	}

	/**
	 * 判断字符串是否为邮箱
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isEmail(String str) {
		boolean bol = false;
		if (str == null || "".equals(str)) {
			return bol;
		}
		String check = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
		bol = str.matches(check);
		return bol;
	}

	/**
	 * 转换为MD5
	 * 
	 * @param str
	 * @return
	 * @throws NoSuchAlgorithmException
	 */
	public static String changeMD5(String str) {
		String result = "";
		try {
			if (!isNotEmpty(str)) {
				return result;
			}
			MessageDigest md = MessageDigest.getInstance("MD5");
			md.update(str.getBytes());
			byte b[] = md.digest();
			int i;
			StringBuffer buf = new StringBuffer("");
			for (int offset = 0; offset < b.length; offset++) {
				i = b[offset];
				if (i < 0) {
					i += 256;
				}
				if (i < 16) {
					buf.append("0");
				}
				buf.append(Integer.toHexString(i));
			}
			result = buf.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 获取文件名的扩展?
	 * 
	 * @param filePath
	 * @return
	 */
	public static String getFileExt(String filePath) {
		String ext = "";
		if (!StringUtil.isNotEmpty(filePath)) {
			return ext;
		}
		int index = filePath.lastIndexOf(".");
		if (index < 1) {
			return ext;
		}
		ext = filePath.substring(index, filePath.length());
		ext = ext.replace(".", "");

		return ext;
	}

	/**
	 * 验证是否为大陆身份证
	 * 
	 * @param arrIdCard
	 * @return
	 */
	public static boolean isIdCard(String arrIdCard) {
		int sigma = 0;
		int[] a = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 };
		String[] w = { "1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2" };
		for (int i = 0; i < 17; i++) {
			int ai = Integer.parseInt(arrIdCard.substring(i, i + 1));
			int wi = a[i];
			sigma += ai * wi;
		}
		int number = sigma % 11;
		String check_number = w[number];
		if (!arrIdCard.substring(17).equals(check_number)) {
			return false;
		} else {
			return true;
		}
	}

	/**
	 * 把Clob类型转成String类型
	 * @param clob
	 * @return
	 */
	public static String parseClobToString(Clob clob) {
		if (clob == null) {
			return null;
		}
		try {
			Reader inStreamDoc = clob.getCharacterStream();
			char[] tempDoc = new char[(int) clob.length()];
			inStreamDoc.read(tempDoc);
			inStreamDoc.close();
			return new String(tempDoc);
		} catch (IOException e) {
			e.printStackTrace();
		} catch (SQLException es) {
			es.printStackTrace();
		}
		return null;
	}

	/**
	 * 提取URL地址参数
	 * @param url
	 * @return
	 */
	public static Map<String,String> subPageParam(String url){
		Map<String,String> result = null;
		if(!StringUtil.isNotEmpty(url)){
			return result;
		}
		int paramMark = url.indexOf('?');
		if(paramMark<0){
			return result;
		}
		String paramStr = url.substring(paramMark+1);
		String[] paramArray = paramStr.split("&");
		String[] paramItem = null;
		if(paramArray!=null){
			for(int i = 0; i < paramArray.length; i++){
				paramItem = paramArray[i].split("=");
				if(paramItem.length==2){
					if(result==null){
						result = new HashMap<String, String>();
					}
					result.put(paramItem[0], paramItem[1]);
				}
			}
		}
		return result;
	}
	
	/**
	 * 转码
	 * @param str
	 * @param enc1
	 * @param enc2
	 * @return
	 */
	public static String changeCode(String str,String enc1,String enc2){
		if (!StringUtil.isNotEmpty(str)) {
			return "";
		}
		try {
			str = new String(str.getBytes(enc1),enc2);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return str;
	}
	
	/**
	 * 转码
	 * @param str
	 * @param enc1
	 * @param enc2
	 * @return
	 */
	public static String changeCode(String str,String enc1){
		if (!StringUtil.isNotEmpty(str)) {
			return "";
		}
		try {
			str = new String(str.getBytes(enc1));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return str;
	}
	
	/**
	 * 验证是否为手机号码
	 * @param mobiles
	 * @return
	 */
	public static boolean isMobile(String mobiles){  
		if (mobiles == null || "".equals(mobiles)) {
			return false;
		}
		Pattern p = Pattern.compile("^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$");  
		Matcher m = p.matcher(mobiles);  
		return m.matches();  
	}
	
	/**
	 * base64转str
	 * @param str
	 * @return
	 */
	public static String decodeBase64 (String str){
		if(!StringUtil.isNotEmpty(str)){
			return "";
		}
		str = new String(Base64.decode(str, Base64.DEFAULT));
		return str;
	}

}
